package model;
import java.awt.Color;
import java.util.List;
import java.util.Iterator;
import java.util.Random;

/**
 * A simple model of a fox.
 * Foxes age, move, eat rabbits, and die.
 * 
 * @author Arjan Frans, Alwin Rombout, Jelte Verbree, Brendan Kanters
 * @version 1.0
 */
public class Fox extends Animal
{
    // Characteristics shared by all foxes (static fields).
    
    // The age at which a fox can start to breed.
    private static int BREEDING_AGE;
    // The age to which a fox can live.
    private static int MAX_AGE;
    // The likelihood of a fox breeding.
    private static double BREEDING_PROBABILITY;
    // The maximum number of births.
    private static int MAX_LITTER_SIZE;
    // The food value of a single rabbit. In effect, this is the
    // number of steps a fox can go before it has to eat again.
    private static int RABBIT_FOOD_VALUE;
    
   
	private Color color = Color.blue;

    /**
     * Create a fox. A fox can be created as a new born (age zero
     * and not hungry) or with a random age and food level.
     * 
     * @param randomAge If true, the fox will have random age and hunger level.
     * @param field The field currently occupied.
     * @param location The location within the field.
     */
    public Fox(boolean randomAge, Field field, Location location)
    {
        super(field, location);
        if(randomAge) {
            age = rand.nextInt(MAX_AGE);
            foodLevel = rand.nextInt(RABBIT_FOOD_VALUE);
        }
        else {
            age = 0;
            foodLevel = RABBIT_FOOD_VALUE;
        }
    }
    
    /**
     * This is what the fox does most of the time: it hunts for
     * rabbits. In the process, it might breed, die of hunger,
     * or die of old age.
     * @param field The field currently occupied.
     * @param newFoxes A list to add newly born foxes to.
     */
    public void act(List<Actor> newFoxes)
    {
    	super.incrementZiekte();
    	super.incrementAge();
        incrementHunger();
        if(isZiek){
        	color = new Color(128,0,128);
        }
        else{
        	color = Color.blue;
        }
        if(isAlive()) {
            giveBirth(newFoxes);            
            // Move towards a source of food if found.
            Location location = getLocation();
            Location newLocation = findFood(location);
            if(newLocation == null) { 
                // No food found - try to move to a free location.
            	newLocation = getField().freeAdjacentLocation(location);
            }
            // See if it was possible to move.
            if(newLocation != null) {
                setLocation(newLocation);
            }
            else {
                // Overcrowding.
                setDead();
                return;
            }
            
        }
    }

    

    
    /**
     * Tell the fox to look for rabbits adjacent to its current location.
     * Only the first live rabbit is eaten.
     * @param location Where in the field it is located.
     * @return Where food was found, or null if it wasn't.
     */
    private Location findFood(Location location)
    {
        Field field = getField();
        List<Location> adjacent = field.adjacentLocations(getLocation());
        Iterator<Location> it = adjacent.iterator();
        while(it.hasNext()) {
            Location where = it.next();
            Object animal = field.getObjectAt(where);
            if(animal instanceof Rabbit) {
                Rabbit rabbit = (Rabbit) animal;
                if(rabbit.isAlive()) { 
                    if(rabbit.getIsZiek()){
                    	setIsZiek(true);
                    }
                	rabbit.setDead();
                    foodLevel = RABBIT_FOOD_VALUE;
                    // Remove the dead rabbit from the field.
                    return where;
                }
            }
        }
        return null;
    }

       
    
    /**
     * @return the food level of the food
     */
    public int getFoodLevel()
    {
    	return foodLevel;
    }

    
    /**
     * @return Max age of a fox
     */
	public int getMaxAge() {
		return MAX_AGE;
	}

	public static int getMaxAgeGUI() {
		return MAX_AGE;
	}
	
    /**
     * @return Age of a fox that can breed
     */
	public int getBreedingAge() {
		return BREEDING_AGE;
	}
	
	public static int getBreedingAgeGUI() {
		return BREEDING_AGE;
	}

	/**
	 * @return BREEDING_PROBABILITY
	 */
	public double getBreedingProbability() {
		return BREEDING_PROBABILITY;
	}
	
	/**
	 * 
	 * @return BREEDING_PROBABILITY
	 */
	public static double getBreedingProbabilityGUI() {
		return BREEDING_PROBABILITY;
	}

	/**
	 * @return MAX_LITTER_SIZE
	 */
	public int getMaxLitterSize() {
		return MAX_LITTER_SIZE;
	}
	
	/**
	 * @return MAX_LITTER_SIZE
	 */
	public static int getMaxLitterSizeGUI() {
		return MAX_LITTER_SIZE;
	}
	
	/**
	 * 
	 * @return RABBIT_FOOD_VALUE
	 */
	public static int getRabbitFoodValue() {
		return RABBIT_FOOD_VALUE;
	}
	
	/**
	 * set the MaxAge
	 * @param maxAge
	 */
	public static void setMaxAge(int maxAge) {
		MAX_AGE = maxAge;
	}
	
	/**
	 * set the breeding age
	 * @param breedingAge
	 */
	public static void setBreedingAge(int breedingAge) {
		BREEDING_AGE = breedingAge;
	}

	/**
	 * set the breeding probability
	 * @param breedingProbability
	 */
	public static void setBreedingProbability(double breedingProbability) {
		BREEDING_PROBABILITY = breedingProbability;
	}
	
	/**
	 * set the max litter size
	 * @param maxLitterSize
	 */
	public static void setMaxLitterSize(int maxLitterSize) {
		MAX_LITTER_SIZE = maxLitterSize;
	}
	
	/**
	 * set the rabbit food value
	 * @param rabbitFoodValue
	 */
	public static void setRabbitFoodValue(int rabbitFoodValue) {
		RABBIT_FOOD_VALUE = rabbitFoodValue;
	}

	@Override
	/**
	 * overrides the getColor() method
	 */
	public Color getColor() {
		return color ;
	}
}

